/**
 * index.html 需要先引入 BrowserPrint-3.0.216.min.js
 */
function XtPrint() {
  this.selected_device = null;
  this.devices = [];

  this.getDevice = () =>
    new Promise((resolve, reject) => {
      this.selected_device = null;
      this.devices = [];
      // Get the default device from the application as a first step. Discovery takes longer to complete.
      BrowserPrint.getDefaultDevice(
        'printer',
        (device) => {
          // Add device to list of devices and to html select element
          this.selected_device = device;
          this.devices.push(device);
          // Discover any other devices available to the application
          BrowserPrint.getLocalDevices(
            (device_list) => {
              for (let i = 0; i < device_list.length; i++) {
                // Add device to list of devices and to html select element
                const device = device_list[i];
                if (!this.selected_device || device.uid !== this.selected_device.uid ) {
                  this.devices.push(device);
                }
                console.log(this, this.selected_device, device);
                resolve(this);
              }
            },
            () => {
              console.log('Error getting local devices');
              reject();
            },
            'printer',
          );
        },
        (error) => {
          console.log(error);
          reject();
        },
      );
    });

  const readCallback = (readData) => {
    if (readData === undefined || readData === null || readData === '') {
      console.log('No Response from Device');
    } else {
      console.log(readData);
    }
  };

  const errorCallback = (errorMessage) => {
    console.log('Error: ' + errorMessage);
  };

  this.getConfig = () => {
    BrowserPrint.getApplicationConfiguration(
      (config) => {
        console.log(JSON.stringify(config));
      },
      () => {
        console.log(JSON.stringify(new BrowserPrint.ApplicationConfiguration()));
      },
    );
  };

  this.writeToSelectedPrinter = (dataToWrite) => {
    this.selected_device.send(dataToWrite, undefined, errorCallback);
  };

  this.readFromSelectedPrinter = () => {
    this.selected_device.read(readCallback, errorCallback);
  };

  this.getDeviceCallback = (deviceList) => {
    console.log('Devices: \n' + JSON.stringify(deviceList, null, 4));
  };

  this.sendImage = (imageUrl) => {
    const url = window.location.href.substring(0, window.location.href.lastIndexOf('/')) + '/' + imageUrl;
    this.selected_device.convertAndSendFile(url, undefined, errorCallback);
  };

  this.sendFile = (fileUrl) => {
    const url = window.location.href.substring(0, window.location.href.lastIndexOf('/')) + '/' + fileUrl;
    this.selected_device.sendFile(url, undefined, errorCallback);
  };

  this.onDeviceSelected = (selected) => {
    for (let i = 0; i < this.devices.length; ++i) {
      if (selected.value === this.devices[i].uid) {
        this.selected_device = this.devices[i];
        return;
      }
    }
  };
}

export const xtPrint = new XtPrint();
